/**
 * Created by lili on 2017/8/4.
 */
/**
 * Created by lili on 2017/8/4.
 */
//https://github.com/addyosmani/pubsubz


(function(define){
    define([], function(){
        "use strict";
        var ap = [],
            nop = function(){};
        function dispatch(list, target, args){
            list = list.slice();

            var i = list.length, caller;
            // the lists are reversed, so we can reverse loop
            // through them
            while((caller = list[--i])){
                (!caller.paused) && caller(target, args);
            }
        }
        function createAspect(list, method){
            return function(listener){
                function caller(target, args){
                    listener.apply(target, args);
                }
                method.call(list, caller);
                var h = {
                    cancel: function(){
                        var i = list.length;
                        while(i--){
                            if(list[i] === caller){
                                list.splice(i, 1);
                                break;
                            }
                        }
                    },
                    resume: function(){
                        caller.paused = false;
                    },
                    pause: function(){
                        caller.paused = true;
                    }
                };
                return h;
            };
        }
        function aspect(type){

            return function(target, methodName, advice){
                var existing = target[methodName], dispatcher;

                if(!existing || !existing.after){
                    // no dispatcher in place
                    var before = [], after = [];
                    dispatcher = target[methodName] = function(){
                        dispatch(before, this, arguments);
                        // call the original method
                        var result = existing && existing.apply(this, arguments);
                        dispatch(after, this, arguments);
                        return result;
                    };

                    // pass the opposite function so we can reverse
                    // loop through the lists
                    dispatcher.before = createAspect(before, ap.push);
                    dispatcher.after = createAspect(after, ap.unshift);
                }
                return (dispatcher || existing)[type](advice);
            };
        }
        return {
            after: aspect("after"),
            before: aspect("before")
        };
    });
})(typeof define != "undefined" ? define : function(deps, factory){
        // if directly loaded, make uber_aop a global
        this.uber_aop = factory();
});

